<?php abstract class pgsql implements iDBDriver {
	protected static $Handle     = NULL;
	protected static $ConnStr    = NULL;
	protected static $Table      = NULL;
	protected static $FetchLimit = NULL;
	protected static $Qs = array (
		'Fetch' =>    'select *, extract (epoch from "time") as "seconds" from "%table%" where not "seen" order by "id" asc limit %fetch_limit%',
		'See' =>      'update "%table%" set "seen" = TRUE where "id" in (%ids%)',
		'Unsee' =>    'update "%table%" set "seen" = FALSE where "seen"',
		'DropSeen' => 'delete from "%table%" where "seen"',
		'Know' =>     'update "%table%" set "known" = TRUE where "id" in (%ids%)',
		'Drop' =>     'delete from "%table%" where "id" in (%ids%)',
	);
	protected static function Query (array $Ids = array ()) {
		$Trace = debug_backtrace ();
		if ((count () < 3) or (! array_key_exists ('function', $Trace [2])) or (! array_key_exists ($QName = $Trace [2]['function'], self::$Qs))) return (NULL);
		$NewIds = array ();
		foreach ($Ids as $Id) array_push ($NewIds, (int) $Id);
		$Query = str_replace (array ('%table%', '%fetch_limit%', '%ids%'), array (self::$Table, self::$FetchLimit, implode (',', $NewIds)), self::$Qs [$QName]);
		return (pg_query (self::$Handle, $Query));
	}
	public static function Open ($Host, $Port, $Database, $User, $Password, $Table, $FetchLimit) {
		$ConnStr = "host=$Host port=$Port dbname=$Database user=$User password=$Password";
		if ($ConnStr === self::$ConnStr) {
			Out::I ("DB not reconnecting to: $ConnStr");
			return (TRUE);
		}
		$Handle = @pg_pconnect ($ConnStr);
		if (FALSE === $Handle) {
			Out::W ("DB connection FAIL to: $ConnStr");
			return (FALSE);
		}
		list (self::$Handle, self::$ConnStr) = array ($Handle, $ConnStr);
		Out::I ("DB connection established to: $ConnStr");
		return (TRUE);
	}
	public static function Drop (array $Ids) {
		if (! count ($Ids)) return (0);
		return (pg_affected_rows (self::Query ($Ids)));
	}
	public static function See (array $Ids) {
		if (! count ($Ids)) return (0);
		return (pg_affected_rows (self::Query ($Ids)));
	}
	public static function Know (array $Ids) {
		if (! count ($Ids)) return (0);
		return (pg_affected_rows (self::Query ($Ids)));
	}
	public static function Fetch () {
		return (pg_fetch_all (self::Query ()));
	}
	public static function Unsee () {
		return (pg_affected_rows (self::Query ()));
	}
	public static function DropSeen () {
		return (pg_affected_rows (self::Query ()));
	}
	public static function __GetRequiredExtensions () {return ('pgsql');}
} ?>